package com.scrx.ymg.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.scrx.ymg.entity.AgentInfo;
import com.scrx.ymg.entity.BodyCount;
import com.scrx.ymg.entity.OrderForm;
import com.scrx.ymg.entity.OtherInfo;
import com.scrx.ymg.entity.ProductEvaluate;
import com.scrx.ymg.entity.SeckillProduct;
import com.scrx.ymg.service.entrepot.ISuperMarketEntrepotService;
import com.scrx.ymg.service.seckill.ISeckillService;

@Controller
@RequestMapping("/seckillAction")
public class SeckillAction {
	
	@Autowired
	private ISeckillService kills;
	@Autowired
	private ISuperMarketEntrepotService smes;
	
	/**
	 * 新建秒杀广告
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/insertSeckillOne",produces = "text/json;charset=UTF-8")
	public String insertSeckillOne(SeckillProduct product,MultipartFile productFile,MultipartFile publicityFile,MultipartFile detailFile){
		return 	kills.insertSeckillOne(product,productFile,publicityFile,detailFile);
	}
	
	/**
	 * 修改秒杀广告
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/updateSeckillOne",produces = "text/json;charset=UTF-8")
	public String updateSeckillOne(SeckillProduct product,MultipartFile productFile,MultipartFile publicityFile,MultipartFile detailFile){
		return 	kills.updateSeckillOne(product,productFile,publicityFile,detailFile);
	}
	/**
	 * 删除秒杀广告
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/deleteSeckillOne",produces = "text/json;charset=UTF-8")
	public String deleteSeckillOne(SeckillProduct product){
		return 	kills.deleteSeckillOne(product);
	}
	
	/**
	 * 查询秒杀广告
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/selectSeckillProductList",produces = "text/json;charset=UTF-8")
	public String selectSeckillProductList(SeckillProduct product,int pageNum,int pageCount){
		Map<Object,Object> map =kills.selectSeckillProductList(product,pageNum,pageCount,null,null);
		Gson gson = new Gson();
		return 	gson.toJson(map);
	}
	
	/**
	 * 查询秒杀广告
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/selectSeckillProductByTime",produces = "text/json;charset=UTF-8")
	public String selectSeckillProductByTime(SeckillProduct product,String startTime,String endTime,int pageNum,int pageCount){
		Map<Object,Object> map =kills.selectSeckillProductList(product,pageNum,pageCount,startTime,endTime);
		Gson gson = new Gson();
		return 	gson.toJson(map);
	}
	
	
	/**
	 * 根据代理区域得到秒杀商品集合
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/getSeckillList",produces = "text/json;charset=UTF-8")
	public String getSeckillList(int agentID){
		List<SeckillProduct> seckills  =kills.getSeckillList(agentID);
		Gson gson = new Gson();
		return 	gson.toJson(seckills);
	}
	
	/**
	 * 根据代理区域编码得到秒杀商品集合
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/getSeckillListByPostalcode",produces = "text/json;charset=UTF-8")
	public String getSeckillListByPostalcode(String postId){
		List<SeckillProduct> seckills  =kills.getSeckillListByPostalcode(postId);
		
		Gson gson = new Gson();
		return 	gson.toJson(seckills);
	}
	
	/**
	 * 根据商品id得到秒杀商品
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/getSeckillOne",produces = "text/json;charset=UTF-8")
	public String getSeckillOne(int id){
		SeckillProduct seckill  =kills.getSeckillOne(id);
		
		OtherInfo info =kills.getOtherInfoById(seckill.getOtherId());
		List<ProductEvaluate> list =smes.getProductEvaluateByIdAndType(id,2);
		Map<Object,Object> map=new HashMap<Object, Object>();
		map.put("SeckillProduct", seckill);
		map.put("Evaluates", list);
		map.put("OtherInfo", info);
		Gson gson = new Gson();
		return gson.toJson(map);
	}
	
	
	/**
	 * 根据代理区域得到团购商品
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/getSeckillProductByAgent",produces = "text/json;charset=UTF-8")
	public String getSeckillProductByAgent(SeckillProduct product,String postId,int pageNum,int pageCount){
		Map<Object, Object> map = kills.getSeckillProductByAgent(product,postId,pageNum,pageCount);
		Gson gson = new Gson();
		return 	gson.toJson(map);
	}
	
	
	/**
	 * 根据秒杀商品id和用户id生成秒杀订单
	 * @param seckillID 秒杀商品ID
	 * @param userID  用户id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/createSeckillOrderForm",produces = "text/json;charset=UTF-8")
	public String createSeckillOrderForm(int seckillID,int userID,int num,int typeId){
		String msg=kills.createSeckillOrderForm(seckillID,userID,num,typeId);
		return msg;
	}
	
	/**
	 * 根据用户查询秒杀商品
	 * @param userID  用户id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/getSeckillOrderForm",produces = "text/json;charset=UTF-8")
	public String getSeckillOrderForm(int userID,int productId){
		return 	kills.getSeckillOrderForm(userID,productId);
	}
	
	/**
	 * 得到异业商家分页
	 * @param info
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/getOtherInfoPage",produces = "text/json;charset=UTF-8")
	public String getOtherInfoPage(OtherInfo info,int pageNum,int pageCount){
		Map<Object, Object> map = kills.getOtherInfo(info,pageNum,pageCount);
		Gson gson = new Gson();
		return 	gson.toJson(map);
	}
	
	/**
	 * 得到异业商家
	 * @param info
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/getOtherInfo",produces = "text/json;charset=UTF-8")
	public String getOtherInfo(OtherInfo info){
		List<OtherInfo> seckills = kills.getOtherInfo(info);
		Gson gson = new Gson();
		return 	gson.toJson(seckills);
	}
	/**
	 * 得到一个异业商家
	 * @param info
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/getOtherInfoOne",produces = "text/json;charset=UTF-8")
	public String getOtherInfoOne(OtherInfo info){
		List<OtherInfo> seckills = kills.getOtherInfo(info);
		Gson gson = new Gson();
		return 	gson.toJson(seckills.get(0));
	}
	
	/**
	 * 统计秒杀人数
	 * @param userId
	 * @param agentId
	 */
	@ResponseBody
	@RequestMapping(value ="/bodyCount",produces = "text/json;charset=UTF-8")
	public void bodyCount(BodyCount bodyCount){
		kills.insertbodyCount(bodyCount);
	}
	
	/**
	 * 更新秒杀商品,广告状态
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/checkAdById",produces = "text/json;charset=UTF-8")
	public String checkAdById(int seckillId,int status){
		return kills.checkAdById(seckillId,status);
	}
	

}
